import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({
    easing: 'ease',  // 动画方式
    speed: 500,  // 递增进度条的速度
    showSpinner: true, // 是否显示加载ico
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3 // 初始化时的最小百分比
})
Vue.use(NProgress)

Vue.use(VueRouter)

const routes = [{
    path: '/login',
    name: 'Login',
    component: () =>
        import ('../components/Login.vue')
},
    {
        path: '/home',
        name: 'Home',
        redirect: '/welcome',
        component: () =>
            import ('../views/Home'),
        children: [
            {
                path: '/welcome',
                component: () =>
                    import ('../components/Welcome')
            },
            {
                path: '/userManager',
                component: () =>
                    import ('../components/users/Users')
            },
            {
                path: '/goodsManager',
                component: () =>
                    import ('../components/goods/goodsManager')
            },
            {
                path: '/categoryManager',
                component: () => import('../components/category/categoryManager')
            },
            {
                path: '/adminMenuManager',
                component: () => import('../components/adminMenu/MenuManager')
            }, {
                path: '/orderManager',
                component: () => import('../components/order/OrderManager')
            }, {
                path: '/newsManager',
                component: () => import('../components/news/newsManager')
            }
        ],
    }
]

const router = new VueRouter({
    routes
})


//路由导航守卫
router.beforeEach((to, from, next) => {
    NProgress.start()
    if (to.path === '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

router.afterEach(() => {
    NProgress.done()
})
export default router